package com.example.yskjproject.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.yskjproject.R;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.CardMediaModel;
import com.example.yskjproject.model.CardModel;
import com.example.yskjproject.model.FollowDateModel;
import com.example.yskjproject.model.FollowModel;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.utils.EventBusUtil;
import com.example.yskjproject.utils.SpCashUtils;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.makeramen.roundedimageview.RoundedImageView;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import es.dmoral.toasty.Toasty;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * ContentAdapter  ---瀑布流---
 * <p>
 * 作者：zx
 * 创建时间：2024/4/18
 */
public class FollowAdapter extends BaseQuickAdapter<FollowDateModel.FollowListBean, BaseViewHolder> {
    private Context context;

    private Gson gson = new Gson();
    private int mScreenWidth;

    public FollowAdapter(Context context, @Nullable List<FollowDateModel.FollowListBean> data, int screenWidth) {
        super(data);
        this.context = context;
        mScreenWidth = screenWidth;
    }
    public void setRecyclerViewWidth(int width) {
        this.mScreenWidth  = width;
        // 你可以在这里做进一步的处理，比如计算每个 Item 的宽度
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view1 = inflater.inflate(R.layout.itme_pic_vid, parent, false);
        return new ViewHolder1(view1);
    }

    @SuppressLint("NewApi")
    @Override
    protected void convert(BaseViewHolder helper, FollowDateModel.FollowListBean item) {
        AppCompatTextView loveTv = helper.getView(R.id.love_tv);
        loveTv.setOnClickListener( love ->{
            Map<String, Object> jsonParams = new HashMap<>();
            jsonParams.put("studio_id", item.getId());
            String paramsDate = gson.toJson(jsonParams);
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsDate);
            OkGo.<LzyResponse<FollowModel>>post(Urls.FOLLOW).upRequestBody(requestBody).execute(new JsonCallback<LzyResponse<FollowModel>>() {
                @SuppressLint("SimpleDateFormat")
                @Override
                public void onSuccess(Response<LzyResponse<FollowModel>> response) {
                    FollowModel.FollowInfoBean followModel = response.body().data.getFollow_info();
                    if (followModel != null) {
                        if (followModel.getState() == 1) {
                            EventBusUtil.sendEvent(new EventMessage(EventContent.follow_list_data));
                        }
                    }
                }
                @Override
                public void onError(Response<LzyResponse<FollowModel>> response) {
                    super.onError(response);
                    ToastUtil.toastShortMessage("点赞失败");
                }
            });
        });
        helper.setText(R.id.woks_des, item.getDescription());
        helper.setText(R.id.name_tv, item.getTitle());
        if (item.getCourse_info() == null ){
            return;
        }

        if (item.getCourse_info().getTag_lv1_id() == 1) {
            //id  = 1
            helper.setText(R.id.tv_position_status, "私教");
        } else if (item.getCourse_info().getTag_lv1_id() == 2) {
            //id  = 2
            helper.setText(R.id.tv_position_status, "团课");
        } else if (item.getCourse_info().getTag_lv1_id() == 3) {
            //id  = 3
            helper.setText(R.id.tv_position_status, "陪练");
        } else if (item.getCourse_info().getTag_lv1_id() == 4) {
            //id  = 4
            helper.setText(R.id.tv_position_status, "直播");
        }

        switch (item.getCourse_info().getState()) {
            case 0:
                helper.setText(R.id.kc_state, "未开课");
                break;
            case 1:
                helper.setText(R.id.kc_state, "售卖中");
                break;
            case 2:
                helper.setText(R.id.kc_state, "已满员");
                break;
            case 3:
                helper.setText(R.id.kc_state, "上课中");
                break;
            case 4:
                helper.setText(R.id.kc_state, "已下课");
                break;
            case -1:
                helper.setText(R.id.kc_state, "下线");
                break;
        }
        helper.setText(R.id.tv_love_num, String.valueOf(item.getFollow_num()));
        RoundedImageView imageView = helper.getView(R.id.card_image);
        PlayerView playerView = helper.getView(R.id.play_view);
        if (!item.getMedia_list().isEmpty()) {
            if (item.getMedia_list().get(0).getMedia_type().equals("Movies")) {
                playerView.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.GONE);
                SimpleExoPlayer mPlayer = new SimpleExoPlayer.Builder(context).build();
                playerView.setPlayer(mPlayer);
                MediaItem mediaItem = MediaItem.fromUri(item.getMedia_list().get(0).getMedia_url());
                mPlayer.setMediaItem(mediaItem);
                mPlayer.play();
            } else {
                playerView.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
                int imageHeight = item.getMedia_list().get(0).getHeight();
                int imageWidth = item.getMedia_list().get(0).getWidth();
                int newHeight = (int) (mScreenWidth/3.0 * (float) imageHeight / (float) imageWidth);
                ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                layoutParams.height = newHeight;
                imageView.setLayoutParams(layoutParams);
                Glide.with(context)
                        .load(item.getMedia_list().get(0).getMedia_url())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(imageView);
            }
        }
        helper.addOnClickListener(R.id.dis_look_works);
    }




    // 定义 ViewHolder
    public static class ViewHolder1 extends BaseViewHolder {
        public ViewHolder1(View view) {
            super(view);
            // 初始化布局1的控件
        }
    }

}
